package com.example.demo.controller;

import com.example.demo.entity.Result;
import com.example.demo.entity.vo.RecordVo;
import com.example.demo.entity.vo.StudentVo;
import com.example.demo.exception.LoginException;
import com.example.demo.service.RecordService;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/record" )
@CrossOrigin

public class RecordController {
    @Autowired
    RecordService recordService;
    @Autowired
    StudentService studentService;

    /**
     * 获取借书纪录
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/getRecord")
    @ResponseBody
    public Result getRecord(HttpServletRequest httpServletRequest) throws LoginException {
        Object id = httpServletRequest.getAttribute("id");
        int id1 = Integer.parseInt(id+"");
        StudentVo message = studentService.getMessage(id1);
        List<RecordVo> record = recordService.getRecord(message.getCardId());
        Result result;
        if (record.size() != 0){
           result = Result.ok("查看成功");
        }else {
            result = Result.ok("查看成功 没有借书纪录");
        }
        result.setData(record);
        return result;
    }


    /**
     * 根据书籍名称获取纪录  模糊查询
     * @param name
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/getRecordByName")
    @ResponseBody
    public Result getRecordByName(String name,HttpServletRequest httpServletRequest,String author) throws LoginException {
        Object id = httpServletRequest.getAttribute("id");
        int id1 = Integer.parseInt(id+"");
        StudentVo message = studentService.getMessage(id1);
        List<RecordVo> recordByBookName = recordService.getRecordByBookName(name, message.getCardId(),author);
        Result result;
        if (recordByBookName.size() != 0){
            result = Result.ok("查看成功");
        }else {
            result = Result.ok("查看成功 没有借书纪录");
        }
        result.setData(recordByBookName);
        return result;
    }



}
